/**
 * 
 */
package design_pattern.observer1;

import java.util.Iterator;
import java.util.Vector;

/**
 * @author linzq
 * 
 */
public class ConcreteSubject implements Subject {

  private Vector<Observer> observers = new Vector<>();

  private synchronized void add(Observer observer) {
    if (!observers.contains(observer)) {
      observers.add(observer);
    }
  }

  @Override
  public void attach(Observer observer) {
    if (!observers.contains(observer)) {
      this.add(observer);
    }
  }

  @Override
  public void detach(Observer observer) {
    observers.remove(observer);
  }

  @Override
  public void notifyObservers() {
    Iterator<Observer> i = observers.iterator();
    Observer temp = null;
    while (i.hasNext()) {
      temp = i.next();
      temp.update();
    }

  }

}
